import { showFailToast } from 'vant'

export function throwError(msg, error) {
  showFailToast(error.message || '操作失败！')
  console.warn(msg + ':')
  console.error(error)
}

// 获取url参数
export function getUrlParams(url) {
  const searchParams = new URLSearchParams(url.split('?')[1])
  const params = {}
  for (const [key, value] of searchParams.entries()) {
    params[key] = value
  }
  return params
}

// 用于检测浏览器是否支持操作
export function copyToClipboard(textToCopy) {
  return new Promise((resolve, reject) => {
    if (navigator.clipboard) {
      navigator.clipboard
        .writeText(textToCopy)
        .then(() => {
          console.log('文本已成功复制到剪贴板')
          resolve(true)
        })
        .catch(error => {
          console.error('复制失败:', error)
          reject(false)
        })
    } else {
      const textArea = document.createElement('textarea')
      textArea.value = textToCopy
      document.body.appendChild(textArea)
      textArea.select()
      document.execCommand('copy')
      document.body.removeChild(textArea)
      console.log('文本已成功复制到剪贴板')
      resolve(true)
    }
  })
}
